package com.lyl.lawyer.service.impl;

import com.cn.lyl.lawyer.dao.LawyerMapper;
import com.cn.lyl.lawyer.dao.LawyerRoleDependenceMapper;
import com.cn.lyl.lawyer.dao.LawyerRoleMapper;
import com.cn.lyl.lawyer.model.entity.LawyerModel;
import com.cn.lyl.lawyer.model.entity.LawyerRoleDependenceModel;
import com.cn.lyl.lawyer.model.entity.LawyerRoleModel;
import com.cn.lyl.lawyer.model.vo.LawyerRoleVo;
import com.cn.lyl.lawyer.service.LawyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LawyerServiceImpl implements LawyerService {
    @Autowired
    private LawyerMapper lawyerMapper;
    @Autowired
    private LawyerRoleMapper lawyerRoleMapper;
    @Autowired
    private LawyerRoleDependenceMapper lawyerRoleDependenceMapper;

    @Override
    @Transactional
    public void add(LawyerRoleVo lawyerRoleVo) {
        LawyerModel lawyerModel = new LawyerModel();
        lawyerModel.setLawyerName(lawyerRoleVo.getLawyerName());
        lawyerModel.setLawyerIntro(lawyerRoleVo.getLawyerIntro());
        lawyerModel.setHead(lawyerRoleVo.getHead());
        lawyerMapper.add(lawyerModel);
        LawyerRoleModel byRole = lawyerRoleMapper.findByRole(lawyerRoleVo.getRole());
        LawyerRoleDependenceModel lawyerRoleDependenceModel = new LawyerRoleDependenceModel();
        lawyerRoleDependenceModel.setRoleId(byRole.getId());
        lawyerRoleDependenceModel.setLawyerId(lawyerModel.getId());
        lawyerRoleDependenceMapper.insert(lawyerRoleDependenceModel);
    }

    @Override
    public List<LawyerRoleVo> findAll(LawyerRoleVo lawyerRoleVo) {


        List<LawyerRoleVo> all = lawyerMapper.findAll(lawyerRoleVo);
        for (LawyerRoleVo vo : all) {

            if(vo.getIsHome().equals(0)){

                vo.setIsHomeValue("否");
            }
            vo.setIsHomeValue("是");
        }
        return all;
    }

    @Override
    public List<LawyerRoleVo> findIsHome(LawyerRoleVo lawyerRoleVo) {
        return  lawyerMapper.findIsHome(lawyerRoleVo);
    }

    @Override
    public int getCount(LawyerRoleVo lawyerRoleVo) {
        return lawyerMapper.countFindAll(lawyerRoleVo);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        lawyerMapper.delete(id);
        lawyerRoleDependenceMapper.deleteByLawyerId(id);
    }

    @Override
    public LawyerRoleVo findById(Integer id) {
        return lawyerMapper.findById(id);
    }

    @Override
    @Transactional
    public void change(LawyerRoleVo lawyerRoleVo) {
        LawyerModel lawyerModel = new LawyerModel();
        lawyerModel.setLawyerName(lawyerRoleVo.getLawyerName());
        lawyerModel.setLawyerIntro(lawyerRoleVo.getLawyerIntro());
        lawyerModel.setHead(lawyerRoleVo.getHead());
        lawyerModel.setId(lawyerRoleVo.getId());
        lawyerMapper.update(lawyerModel);
        LawyerRoleModel byRole = lawyerRoleMapper.findByRole(lawyerRoleVo.getRole());
        LawyerRoleDependenceModel lawyerRoleDependenceModel = new LawyerRoleDependenceModel();
        lawyerRoleDependenceModel.setLawyerId(lawyerRoleVo.getId());
        lawyerRoleDependenceModel.setRoleId(byRole.getId());
        lawyerRoleDependenceMapper.update(lawyerRoleDependenceModel);
    }

    @Override
    public void changeHomeState(Integer id) {
        lawyerMapper.updateIsHome(id);

    }

    @Override
    public List<LawyerRoleModel> findAllRole() {

        return lawyerRoleMapper.findAll();
    }

}
